package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.odin.solution.enumcode.activity.HundredStylistSaveStatus;
import com.homedo.odin.solution.model.request.activity.HundredStylistInfoRequestBean;
import com.homedo.odin.solution.model.request.activity.SolutionApplyInfoRequestBean;
import com.homedo.odin.solution.model.response.activity.HundredStylistElectPageResponseBean;
import com.homedo.odin.solution.model.response.activity.SolutionStylistInfoResponseBean;
import com.homedo.odin.solution.service.HundredStylistInfoServiceImpl;
import com.homedo.odin.solution.service.HundredStylistSolutionApplyInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/4/25
 * @Description: PC 活动接口 API
 */
@Api(description = "活动", tags = {"Activity"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private HundredStylistInfoServiceImpl hundredStylistInfoService;
    @Autowired
    private HundredStylistSolutionApplyInfoServiceImpl hundredStylistSolutionApplyInfoService;

    @ApiOperation(value = "PC_百名设计师信息录入接口", httpMethod = "POST")
    @PostMapping(value = "/hundred_stylist/info")
    public ResultData stylistInfo(@Valid @RequestBody HundredStylistInfoRequestBean requestBean,
            BindingResult bindingResult) {
        Integer status = requestBean.getStatus();
        //保存 与 提交都不包含未非法数字
        if (!HundredStylistSaveStatus.dbValueExist(requestBean.getStatus())) {
            return ResultData.newInstance(Meta.CODE_451, "status参数非法：資料提交类型 1：完整资料提交（需走校验） 0：不完整资料提交");
        }

        //完整提交必须走校验
        if (HundredStylistSaveStatus.SUBMIT.getDbValue().equals(status)) {
            if (bindingResult.hasErrors()) {
                String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
                return ResultData.newInstance(Meta.CODE_451, defaultMessage);
            }
        }

        hundredStylistInfoService.stylistInfo(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @ApiOperation(value = "PC_百名设计师提交参评方案接口", httpMethod = "POST")
    @PostMapping(value = "/hundred_stylist/solution_apply")
    public ResultData saveSolutionApplyInfo(@Valid @RequestBody SolutionApplyInfoRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }

        hundredStylistSolutionApplyInfoService.saveSolutionApplyInfo(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @ApiOperation(value = "PC_百名设计师获取用户评选信息详情接口", httpMethod = "GET", response = SolutionStylistInfoResponseBean.class)
    @GetMapping(value = "/hundred_stylist/info/{userId}")
    public ResultData querySolutionStylistInfo(
            @ApiParam(value = "用戶ID", required = true) @PathVariable Integer userId) {
        if (userId == null) {
            throw new BizException(Meta.CODE_451.getCode(), "userId参数非法");
        }

        SolutionStylistInfoResponseBean responseBean = hundredStylistInfoService.querySolutionStylistInfo(userId);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @ApiOperation(value = "PC_百名设计师评选结果接口", httpMethod = "GET", response = HundredStylistElectPageResponseBean.class)
    @GetMapping(value = "/hundred_stylist/elect")
    public ResultData querySolutionStylistElect(
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize) {
        if (pageNum == null || pageNum < 0 || pageSize == null || pageSize < 0) {
            throw new BizException(Meta.CODE_451.getCode(), "分页参数非法");
        }

        HundredStylistElectPageResponseBean responseBean = hundredStylistInfoService
                .querySolutionStylistElect(pageNum, pageSize);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }
}
